layui.use(['form', 'jquery', 'layer', 'table', 'laydate', 'laypage'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var laypage = layui.laypage;
	var upload = layui.upload;

	var table_filter = 'baojia_list',
		form_filter = 'baojia_search_form';
	var where_json = {
		_o: 'baojia_id',
		_b: 'desc'
	};
	// 渲染表格
	table.render({
		elem: '#' + table_filter,
		id: table_filter,
		url: baojia_list_url,
		method: 'post',
		height: 'full-200',
		where: {
			_o: 'baojia_id',
			_b: 'desc'
		},
		cols: [baojia_cols],
		page: {
			layout: ['count', 'first', 'prev', 'page', 'next', 'last', 'skip'],
			limit: 30,
			theme: '#393d49',
			groups: 8
		},
		autoSort: false,
		toolbar: '#baojiaToolbar',
		defaultToolbar: [],
		initSort: {
			field: 'baojia_id',
			type: 'desc'
		},
		done: function(res) {
			res.data.forEach((e, i) => {
				console.log(e.active)
				if (!e.active) {
					$('.layui-table-view[lay-id="baojia_list"] table tbody').find(
							'tr[data-index="' + i + '"]').find('td[data-field="zhenshi"]')
						.attr('data-edit', 'false')
				}
			})
		}
	});

	/* table.on('edit(baojia_list)',function(obj){
		console.log(obj.value)
	}) */


	// 修改真实库存
	upload.render({
		elem: '#updatezhenshi',
		url: update_zhenshi_url,
		acceptMime: 'application/vnd.ms-excel',
		auto: true,
		accept: 'file',
		exts: 'xls',
		multiple: false,
		drag: false,
		before: function(obj) {
			layer.load();
		},
		done: function(res, index, upload) {
			if (res.code == 0) {
				layer.closeAll('loading');
				layer.msg(res.msg, {
					icon: 3
				});
				return false;
			} else {
				var tr = ''
				res.data.forEach((e, i) => {
					tr += `<tr>
						<td>${e.goods_sku}</td>
						<td>${e.state}</td>
						<td>${e.msg}</td>
						</tr>
					`
				})
				$('#zhenshi_info_box').html(tr)
				layer.open({
					type: 1,
					shade: 0,
					area: ['700px', '600px'],
					title: '导入结果', //不显示标题
					content: $('.zhenshi_info'),
				});
				table.reload('baojia_list')
				layer.closeAll('loading');
			}

		},
		error: function(index, upload) {
			layer.msg('excel格式错误', {
				icon: 2
			});
			layer.closeAll('loading');
		}
	});

	// 监听表头按钮
	table.on('toolbar(' + table_filter + ')', function(obj) {
		var checkStatus = table.checkStatus(table_filter);
		if (obj.event != 'exportAllBaojia') {
			if (checkStatus.data.length == 0) {
				layer.msg('请选择报价商品', {
					icon: 2,
					anim: 6
				});
				return false;
			}
			baojia_id_arr = new Array();
			for (var i in checkStatus.data) {
				if (checkStatus.data[i].audit_state != 2) {
					layer.msg('存在已审核的报价商品', {
						icon: 2,
						anim: 6
					});
					return false;
				}
				baojia_id_arr.push(checkStatus.data[i].baojia_id);
			}
		}
		switch (obj.event) {
			case 'exportAllBaojia':
				layer.confirm('确定导出本次查询的订单吗？', {
					yes: function() {
						layer.load(2);
						send_info(export_url, where_json, function(data) {
							layer.closeAll();
							layer.open({
								type: 2,
								title: '下载excel',
								content: data.data
							});
						})
					}
				});
				break;
			case 'allPass':
				layer.confirm('确定审核通过选中报价商品吗？', {
					yes: function() {
						layer.load(2);
						send_info(baojia_all_pass_url, {
							baojia_id_arr: baojia_id_arr
						}, function(data) {
							layer.closeAll();
							layer.msg(data.msg, {
								icon: data.code,
								anim: 6
							});
							table.reload(table_filter);
						})
					}
				});
				break;
			case 'allReject':
				layer.prompt({
					'title': '请输入拒绝原因'
				}, function(value, index, elem) {
					value = value.trim();
					if (value == '') {
						layer.msg('请输入拒绝原因');
						return false;
					}
					layer.load(2);
					send_info(baojia_all_refuse_url, {
						baojia_id_arr: baojia_id_arr,
						remark: value
					}, function(data) {
						layer.closeAll();
						layer.msg(data.msg, {
							icon: data.code,
							anim: 6
						});
						table.reload(table_filter);
					})
				})
				break;
			default:
				break;
		}
		return false;
	})
	form.render();
	$('#order_search_btn').on('click', function() {
		layer.load();
		table.reload(table_filter, {
			where: where_json,
			page: {
				curr: 1
			},
			done: function() {
				layer.closeAll();
			}
		});
	})
	var select_search_list = function(field, value) {
		where_json[field] = value;
	};
	form.on('select', function(data) {
		var value = data.value;
		var e = data.elem;
		var id = $(e).attr('id');
		select_search_list(id, value);
	});
	// 表单input 查询
	$('form[lay-filter="' + form_filter + '"]').on('input propertychange', 'input[type="text"]', function() {
		if ($(this).prop('comStart')) return;
		var value = $(this).val();
		var name = $(this).attr('name');
		select_search_list(name, value);
	});
	var min_create_time = laydate.render({
		elem: 'input[name="min_create_time"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			select_search_list('min_create_time', value);
		}
	});
	var max_create_time = laydate.render({
		elem: 'input[name="max_create_time"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			select_search_list('max_create_time', value);
		}
	});
	var min_audit_time = laydate.render({
		elem: 'input[name="min_audit_time"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			select_search_list('min_audit_time', value);
		}
	});
	var max_audit_time = laydate.render({
		elem: 'input[name="max_audit_time"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			select_search_list('max_audit_time', value);
		}
	});
	var min_zhouqi_time = laydate.render({
		elem: 'input[name="min_zhouqi_time"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			select_search_list('min_zhouqi_time', value);
		}
	});
	var max_zhouqi_time = laydate.render({
		elem: 'input[name="max_zhouqi_time"]',

		theme: '#393D49',
		done: function(value, date, endDate) {
			select_search_list('max_zhouqi_time', value);
		}
	});
	// 表格工具监听
	table.on('edit(' + table_filter + ')', function(obj) {
		let post_data = {
			value: parseInt(obj.value),
			baojia_id: obj.data.baojia_id
		}
		if (!obj.value || isNaN(obj.value) || parseInt(obj.value) < 1) {
			layer.msg('输入错误', {
				icon: 2,
				anim: 6
			});
			return false
		}
		send_info(baojia_edit_url, post_data, function(data) {
			layer.msg('修改成功', {
				icon: 1,
				anim: 6
			});

		})
	})
	table.on('tool(' + table_filter + ')', function(obj) {
		var data = obj.data;
		var e = obj.event;
		var tr = obj.tr;
		var baojia_id = data.baojia_id;
		switch (e) {
			case 'pass':
				layer.confirm("确认审核通过吗?", {
					icon: 3,
					title: '提示'
				}, function(index) {
					send_info(baojia_pass_url, {
						baojia_id: baojia_id
					}, function(data) {
						layer.closeAll();
						layer.msg('操作成功', {
							icon: 1,
							anim: 6
						});
						table.reload(table_filter);
					})
				})
				break;
			case 'refuse':
				layer.prompt({
					title: '拒绝原因'
				}, function(value, index, elem) {
					if (!value) {
						layer.msg('请输入拒绝原因', {
							icon: 2,
							anim: 6
						});
						return false;
					}
					send_info(baojia_refuse_url, {
						baojia_id: baojia_id,
						remark: value
					}, function(data) {
						layer.closeAll();
						layer.msg('操作成功', {
							icon: 1,
							anim: 6
						});
						table.reload(table_filter);
					})
				});
				break;
			default:
				break;
		}
	})
})